package com.liutaw.nanjingair.views.viewcomponent;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.style.AbsoluteSizeSpan;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.liutaw.nanjingair.R;

public class EditTextPlus extends FrameLayout {
    private int mDefaultHintSize = 16;
    private String mHintText;
    private int mLeftImgResId;
    private int mHintSize;
    private View mToolTipView;
    private TextView mToolTip;
    private ImageView mClearView;
    private EditText mEditText;
    private TextChangedCallBack callBack;

    public EditTextPlus(Context context) {
        super(context);
        mHintText = "";
        mLeftImgResId = 0;
        mHintSize = mDefaultHintSize;
    }

    public EditTextPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.EditTextPlus);
        mHintText = typedArray.getString(R.styleable.EditTextPlus_hint_text);
        mLeftImgResId = typedArray.getResourceId(R.styleable.EditTextPlus_hint_left_drawable, 0);
        mHintSize = typedArray.getInteger(R.styleable.EditTextPlus_hint_text_size, mDefaultHintSize);
        initView();
    }

    private void initView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_edittext_plus, null);
        ImageView left_icon = (ImageView) view.findViewById(R.id.icon_left);
        if (mLeftImgResId != 0) {
            left_icon.setImageResource(mLeftImgResId);
        } else {
            left_icon.setVisibility(GONE);
        }
        mClearView = (ImageView) view.findViewById(R.id.btn_clear_edit);
        mEditText = (EditText) view.findViewById(R.id.edit_content);
//        mEditText.clearFocus();
        setHint(mHintText);
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (callBack != null) {
                    callBack.afterChanged();
                }
                clearToolTip();
                if (s.length() != 0) {
                    mClearView.setVisibility(View.VISIBLE);
                    return;
                }
                mClearView.setVisibility(View.INVISIBLE);

            }
        });
        mToolTipView = view.findViewById(R.id.layout_tooltip_view);
        mToolTip = (TextView) view.findViewById(R.id.text_tooltip);
        mClearView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mEditText.setText("");
            }
        });
        view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                getFocusAndSetSelection();
            }
        });
        addView(view);
    }


    private SpannableString getHintString(String hintString) {
        // 新建一个可以添加属性的文本对象
        SpannableString ss = new SpannableString(hintString);
        // 新建一个属性对象,设置文字的大小
        AbsoluteSizeSpan ass = new AbsoluteSizeSpan(mHintSize, true);
        // 附加属性到文本
        ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        // 设置hint
        return new SpannableString(ss);
    }

    public void showTooltip(String toolTip) {
        if (mToolTip != null)
            mToolTip.setText(toolTip);
        if (mToolTipView != null) {
            mToolTipView.setVisibility(VISIBLE);
            ObjectAnimator animator = ViewUtils.nope(mToolTipView);
            animator.start();
        }
        getFocusAndSetSelection();
    }

    public void clearToolTip() {
        if (mToolTipView != null)
            mToolTipView.setVisibility(GONE);
    }

    public void setEnable(boolean isEnable) {
        if (!isEnable) {
            clearToolTip();
            mClearView.setVisibility(View.INVISIBLE);
            mEditText.setEnabled(false);
        } else {
            mEditText.setEnabled(true);
            if (!mEditText.getText().toString().equals("")) {
                mClearView.setVisibility(View.VISIBLE);
            }
        }
    }

    public void setText(String text) {
        mEditText.setText(text);
    }

    public String getText() {
        return mEditText.getText().toString().trim();
    }

    public void setHint(String text) {
        mEditText.setHint(getHintString(text));
    }

    public void getFocusAndSetSelection() {
        mEditText.requestFocus();
        mEditText.setSelection(getText().length());
    }

    public void setInputType(int type) {
        mEditText.setInputType(type);
        if (type == InputType.TYPE_CLASS_PHONE) {
            setMaxLength(11);
        }
    }

    public void setMaxLength(int maxLength) {
        InputFilter[] filters = {new InputFilter.LengthFilter(maxLength)};
        getEditText().setFilters(filters);
    }

    public EditText getEditText() {
        return mEditText;
    }

    public void setOnFocusChangeListener(OnFocusChangeListener onFocusChangeListener) {
        if (onFocusChangeListener == null) return;
        mEditText.setOnFocusChangeListener(onFocusChangeListener);
    }

    public TextChangedCallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(TextChangedCallBack callBack) {
        this.callBack = callBack;
    }

    public interface TextChangedCallBack {
        void afterChanged();
    }

    public void setOnTextChangedListener(final AfterTextChangedCallBack callBack) {
        getEditText().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 0) {
                    callBack.onEmpty();
                    return;
                }
                callBack.onAfterChanged(s.toString());
            }
        });
    }

    public interface AfterTextChangedCallBack {
        void onAfterChanged(String text);

        void onEmpty();
    }

}
